//
//  AdvertiseView.m
//  Gunterapple
//
//  Created by 热带雨林 on 2020/3/18.
//  Copyright © 2020 热带雨林. All rights reserved.
//

#import "AdvertiseView.h"
#import "UIView+AdvertiseView.h"
#import "UIView+extension.h"
#import <WKWebViewJavascriptBridge.h>//添加WKWebViewJavascriptBridged第三方库

@interface AdvertiseView ()

@property (nonatomic, strong) UIImageView *imgView;

@property (nonatomic, strong) UIButton *countBtn;

@property (nonatomic, strong) NSTimer *countTimer;//定时器

@property (nonatomic, assign) int count;

@end

static int const showTime = 3;//显示时间

@implementation AdvertiseView
- (instancetype)initWithFrame:(CGRect)frame{
    if(self = [super initWithFrame:frame]){
        self.backgroundColor = [UIColor redColor];
        //图片广告
        self.imgView = [[UIImageView alloc]initWithFrame:frame];
        self.imgView.userInteractionEnabled = YES;
        self.imgView.contentMode = UIViewContentModeScaleAspectFill;
        self.imgView.clipsToBounds = YES;
        [self.imgView setImage:[UIImage imageNamed:@"laImage"]];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(jumpToAd)];
        [self.imgView addGestureRecognizer:tap];
        //跳过广告
        CGFloat btnH = 60;
        CGFloat btnW = 30;
        self.countBtn = [[UIButton alloc]initWithFrame:CGRectMake(GDScreenW - btnW - 24, btnH, btnW, btnH)];
        [self.countBtn addTarget:self action:@selector(dissmiss) forControlEvents:UIControlEventTouchUpInside];
        [self.countBtn setTitle:[NSString stringWithFormat:@"跳过%d",showTime] forState:UIControlStateNormal];
        self.countBtn.titleLabel.font = [UIFont systemFontOfSize:15];
        [self.countBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        self.countBtn.backgroundColor = [UIColor colorWithRed:38 /255.0 green:38 /255.0 blue:38 /255.0 alpha:0.6];
        self.countBtn.layer.cornerRadius = 4;
        //
        [self addSubview:self.imgView];
        [self addSubview:self.countBtn];
        [self text1];//调用扩展的方法
        self.name = @"hello";
    }
    return self;
}
//通过扩展添加的方法
- (void)text1{
    NSLog(@"HH");
}
- (void)text2{
    NSLog(@"Hello");
}
#pragma mark -
- (void)show{
    [self startTimer];
    //同时兼容iOS13 与 iOS12
//    UIWindow *window = nil;
//    if(@available(iOS 13,*)){
//        window = [[[UIApplication sharedApplication] windows] objectAtIndex:0];
//////        UIWindow *window = [UIApplication sharedApplication].windows[0];
////        for(UIWindowScene *windowScene in [UIApplication sharedApplication].connectedScenes){
////            if(windowScene.activationState == UISceneActivationStateForegroundActive){
////                window = windowScene.windows.firstObject;
////            }
////        }
//    }else{
//        window = [UIApplication sharedApplication].keyWindow;
//    }
//    [window addSubview:self];
}
- (void)startTimer{
    _count = showTime;
    [[NSRunLoop mainRunLoop]addTimer:self.countTimer forMode:NSRunLoopCommonModes];
}
#pragma mark - 跳转到广告
- (void)jumpToAd{
    //启动页消失 发消息跳转到广告
    [self dissmiss];
    //发送消息 弹出加载消息的界面
//    [[NSNotificationCenter defaultCenter]postNotificationName:@"pushtoads" object:nil userInfo:nil];
}
#pragma mark - 跳过广告
- (void)dissmiss{
    [self.countTimer invalidate];//
    self.countTimer = nil;
    [UIView animateWithDuration:0.3f animations:^{
        self.alpha = 0.f;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}
#pragma mark - 自动计时
- (void)countDown{
    self.count --;
    [self.countBtn setTitle:[NSString stringWithFormat:@"跳过%d",self.count] forState:UIControlStateNormal];
    if(self.count == 0){
        //计时完成 广告消失
        [self dissmiss];
    }
}
- (void)setFilePath:(NSString *)filePath{
    _filePath = filePath;
    [self.imgView setImage:[UIImage imageNamed:filePath]];
}
#pragma mark - lazy
- (NSTimer *)countTimer{
    if(!_countTimer){
        _countTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(countDown) userInfo:nil repeats:YES];
    }
    return _countTimer;
}
@end
